<template>
	<view>
		<view style="background-color: white;">
			<view class="item">
				<view class="title">上次抄表度数</view>
				<view class="value">{{strv}}</view>
			</view>
			<view class="item">
				<view class="title">本次抄表度数</view>
				<view class="value">
					<u--input placeholderStyle="text-align: right;" type="digit" inputAlign="right" placeholder="待输入" border="none"
						v-model="endv" fontSize="14px" @change="change" @confirm="confirm"></u--input>
				</view>
			</view>
			<view class="item">
				<view class="title">本次合计使用度数</view>
				<view class="value">{{currentAmount}}</view>
			</view>
			<view class="item" style="border-bottom:transparent">
				<view class="title">当前单价(元/度)</view>
				<view class="value">{{price}}</view>
			</view>
		</view>
		<view style="background-color: white;padding:30rpx;padding-top: 0rpx;margin-top: 30rpx;">
			<view style="line-height: 60rpx;">拍照(ocr识别)</view>
			<view class="flex align-center">
				<view class="uploadImg"
					style="background-color: transparent;border: 1px solid #CCCCCC;margin-right: 20rpx;"
					v-for="key in shop_imagesList" @click="previewImage">
					<image :src="key"></image>
				</view>
				<view class="uploadImg" @click="upFileImg">
					<image src="../../static/images/jiahao.png" style="width: 61rpx;height: 60rpx;"></image>
					<view style="font-size: 24rpx;font-weight: 400;color: #FFFFFF;line-height: 40rpx;">点击上传</view>
				</view>
			</view>
		</view>
		<view class="flex align-center justify-center">
			<view class="tijiao" @click="saveElectric">上传</view>
		</view>
	</view>
</template>

<script>
	import {
		postLandlord
	} from "../../api/user.js";
	import {
		electric
	} from "../../api/home.js";
	import {
		url,
		upFile
	} from "../../utils/url.js";
	export default {
		data() {
			return {
				imgUrl: "",
				detail: {},
				token: '',
				userInfo: {},
				strv: "",
				price: "",
				device_id: "",
				endv: "",
				currentAmount: "待计算",
				shop_imagesList: []
			}
		},
		onLoad(options) {
			//判断用户是否登陆
			if (options != null) {
				this.strv = options.strv;
				this.price = options.price
				this.device_id = options.device_id
			}
			if (!uni.getStorageSync('userInfo')) {
				uni.setStorageSync('isType', '1')
				setTimeout(() => {
					uni.navigateTo({
						url: '/pages/user/login'
					})
				}, 1200)
				return

			} else {
				// 用户已登陆
				this.init()
			}
		},
		methods: {
			previewImage() {
				uni.previewImage({
					urls: this.shop_imagesList,
					longPressActions: {
						itemList: ['发送给朋友', '保存图片', '收藏'],
						success: function(data) {
							console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) + '张图片');
						},
						fail: function(err) {
							console.log(err.errMsg);
						}
					}
				});
			},
			change(e) {
				this.endv = e;
				this.currentAmount = e - this.strv;
			}, 
			confirm(e) {
				if (Number(this.strv)>Number(e)) {
					uni.showToast({
						title: "不可小于上次抄表度数",
						icon: 'none'
					})
				} else {
					this.currentAmount = e - this.strv;
				}
			},
			async init() {
				let data = new Object();
				data.token = uni.getStorageSync('token');

				let res = await postLandlord(data)

				// this.detail = res.data;
				// this.token = uni.getStorageSync('token');
				// this.userInfo = uni.getStorageSync('userInfo')
				// return
				if (res.data.code == 1) {
					// uni.showModal({
					// 	title: "提示",
					// 	content: "恭喜成为房东",
					// 	showCancel: false,
					// 	success() {
					// 		// uni.reLaunch({
					// 		// 	url: "/pages/home/consult"
					// 		// })
					// 	}

					// })
				} else {
					uni.showToast({
						title: res.data.msg,
						icon: 'none'
					})
					if (res.data.code == 401) {
						uni.setStorageSync('isType', '1')
						setTimeout(() => {
							uni.navigateTo({
								url: '/pages/user/login'
							})
						}, 1200)
					}
				}
			},
			async saveElectric() {
				console.log(this.endv);
				uni.showLoading({
					title: ''
				});
				if (this.strv == "") {
					uni.showToast({
						title: "本次抄表度数错误",
						icon: 'none'
					})
					return;
				}
				if (this.device_id == "") {
					uni.showToast({
						title: "设备ID数据错误",
						icon: 'none'
					})
					return;
				}
				if (this.endv == "") {
					uni.showToast({
						title: "未输入本次抄表度数",
						icon: 'none'
					})
					return;
				}
				if (Number(this.strv)>Number(this.endv)) {
					uni.showToast({
						title: "不可小于上次抄表度数",
						icon: 'none'
					})
					return;
				}
				if (this.imgUrl == "") {
					uni.showToast({
						title: "图片未上传",
						icon: 'none'
					})
					return;
				}
				var data = {
					token: wx.getStorageSync("token"),
					strv: this.strv,
					endv: this.endv,
					price: this.price,
					device_id: this.device_id,
					img:this.imgUrl
				}
				electric(data).then(res => {
					uni.hideLoading();
					console.log(res);
					if (res.data.code == 1) {
						uni.showToast({
							title: "上传成功",
							icon: ''
						})
						setTimeout(() => {
							uni.navigateBack({
								delta: 1
							});
						}, 1200)
					} else {
						uni.showToast({
							title: res.data.msg,
							icon: 'error'
						})
					}
				})
			},
			// 上传图片
			upFileImg(key) {
				var that = this;
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: function(res) {
						console.log(JSON.stringify(res));
						let videoFile = res.tempFilePaths[0];
						uni.uploadFile({
							url: upFile,
							method: "POST",
							filePath: videoFile,
							name: 'file',
							success: (res) => {
								res = JSON.parse(res.data)
								uni.hideLoading()
								that.shop_imagesList = [];
								that.imgUrl = "";
								if (res.code == 1) {
									var shop_imagesList = that.shop_imagesList;
									shop_imagesList.push(res.data.fullurl);
									that.shop_imagesList = shop_imagesList;
									that.imgUrl = res.data.url
								} else {
									uni.showToast({
										title: res.msg,
										icon: 'none'
									})
								}
							},
							fail(res) {
								console.log(JSON.stringify(res));
							}
						})
					}
				});

			},

		}
	}
</script>

<style>
	.uploadImg {
		width: 118rpx;
		height: 118rpx;
		background: #ECECEC;
		border-radius: 18rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;
	}

	.item {
		padding: 0 30rpx;
		display: flex;
		line-height: 85rpx;
		border-bottom: 1px solid #DCDCDC;
		align-items: center;
	}

	.title {
		font-size: 28rpx;
		font-family: PingFang SC;
		font-weight: 400;
		color: #000000;
		line-height: 85rpx;
		flex: 1;
	}

	.value {
		font-size: 28rpx;
		font-family: PingFang SC;
		font-weight: 400;
		color: #818181;
		line-height: 85rpx;
	}

	.tijiao {
		width: 568rpx;
		height: 88rpx;
		background: linear-gradient(90deg, #0DE3AC, #98E05F);
		border-radius: 44rpx;
		font-size: 32rpx;
		font-family: PingFang SC;
		font-weight: 400;
		color: #FFFFFF;
		line-height: 69rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		margin-top: 240rpx;

	}
</style>
